package config;

import com.alibaba.druid.pool.DruidDataSource;
import dao.DbConfig;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;

@Configuration
@ComponentScan({"dao", "service", "aspect"})
@EnableAspectJAutoProxy
//classpath:前缀表示在类路径下找
//file:前缀表示在文件系统中找：file：e:/db.properties
//PropertySource不支持classPath*前缀
@PropertySource("classpath:db.properties")
public class AppConfig {

    @Autowired
    private DbConfig dbConfig;

    @Bean
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(dbConfig.getUrl());
        dataSource.setUsername(dbConfig.getUsername());
        dataSource.setPassword(dbConfig.getPassword());
        dataSource.setDriverClassName(dbConfig.getDriverClassName());
        return dataSource;
    }

    /* @Bean
     public DataSource dataSource(){
         DruidDataSource dataSource = new DruidDataSource();
         dataSource.setUrl("jdbc:mysql://localhost:3306/demo");
         dataSource.setUsername("root");
         dataSource.setPassword("root");
         dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
         return dataSource;
     }*/
    @Bean
    public QueryRunner queryRunner(DataSource dataSource) {
        QueryRunner queryRunner = new QueryRunner(dataSource, true);
        return queryRunner;
    }

}
